//
//  myButoon.swift
//  demo01
//
//  Created by babybus on 2025/6/26.
//

import Foundation
import UIKit
import SnapKit

class DefaultCell:UICollectionViewCell{
    
    public var cellButton:UIButton={
        let btn = UIButton();
        btn.setTitleColor(.black, for: .normal);
        btn.addTarget(self, action: #selector(goToNextView), for: .touchUpInside)
        return btn;
    }();
    private let containerStack:UIStackView = {
        let stack = UIStackView()
        stack.axis = .vertical
        stack.alignment = .center
        stack.distribution = .fill
        stack.spacing = 8
        return stack
    }()
    override init(frame: CGRect) {
        super.init(frame: frame);
        setupUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented");
    }

    func configure(btnTitle: String,btnColor: UIColor) {
        cellButton.setTitle(btnTitle, for: .normal)
        cellButton.backgroundColor = btnColor
    }
    private func setupUI(){
        containerStack.addArrangedSubview(cellButton)
        cellButton.translatesAutoresizingMaskIntoConstraints = false

        
        contentView.addSubview(containerStack);
        containerStack.translatesAutoresizingMaskIntoConstraints = false;
        
        containerStack.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(8)
            make.top.bottom.equalToSuperview().inset(8)
        }

    }
    // MARK: - 重用准备
    override func prepareForReuse() {
        super.prepareForReuse()
    }
    //弱引用委托
    weak var delegateTest:MyCellButtonDelegate?

    //委托协议
    protocol MyCellButtonDelegate:AnyObject {
        func didTapButton(in cell:DefaultCell);
    }
    //按钮点击
    @objc private func goToNextView(){
        print("button -- goToNextView")
        delegateTest?.didTapButton(in: self);
    }
}
